<template>
    <div class="auto-archive-container">
        <div class="auto-archive-header">
            <!-- <span class="auto-archive-title">历史报账 </span> -->
            <el-form ref="form" :model="form" label-width="110px" class="auto-archive-body-header">
                <el-form-item label="单号：" class="auto-archive-input">
                    <el-input v-model="form.billId" clearable placeholder="请选择单号" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
                <el-form-item label="申请日期：" class="auto-archive-select">
                    <el-date-picker v-model="form.billSubmitTime" type="date" placeholder="选择日期" clearable>
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="报账类型：" class="auto-archive-select">
                    <el-select v-model="form.billType" clearable placeholder="请选择报账类型" class="auto-archive-select-item">
                        <el-option v-for="(item, $index) in typeList" :key="$index" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-button type="primary" class="auto-archive-button">查询</el-button>
                <el-button type="primary" style="margin-left: 0;" class="auto-archive-button"
                    @click="ToAddBillApplication">新建</el-button>
            </el-form>


        </div>

        <div class="auto-archive-body">

            <div class="auto-archive-horizon"></div>

            <div class="auto-archive-table">
                <el-table :data="this.filtedTableData" style="width: 100%" max-height="450">
                    <el-table-column prop="billId" label="单号" align="center">
                    </el-table-column>
                    <el-table-column prop="userName" label="申请人" align="center">
                    </el-table-column>
                    <el-table-column prop="billSubmitTime" label="申请日期" align="center">
                    </el-table-column>
                    <el-table-column prop="billType" label="报账类型" align="center">
                    </el-table-column>
                    <el-table-column prop="billProgress" label="工单状态" align="center">
                    </el-table-column>

                    <el-table-column prop="action" label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="ToBillDetail(scope.row)">详情</el-button>
                        </template>

                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination">
                <el-pagination background layout="total, sizes, prev, pager, next, jumper" :current-page="tablePage.pageNum"
                    :page-size="tablePage.pageSize" :page-sizes="pageSizes" :total="tablePage.total"
                    @size-change="handleSizeChange" @current-change="handlePageChange" style="padding-right: 30px;" />
            </div>
        </div>

    </div>
</template>

<script>
import { billPageQuery } from '@/api/bill'
export default {
    name: "QueryBills",
    data() {
        return {
            tableData: [],
            form: {
                billId: null,
                billSubmitTime: null,
                billType: null
            },

            typeList: ["用车费用", "维修费用", "保养费用"],
            tablePage: {
                pageNum: 1, // 第几页
                pageSize: 10, // 每页多少条
                total: 0 // 总记录数
            },
            BasicDataPage: null,
            pageSizes: [4, 8],
        }
    },
    computed: {
        filtedTableData() {
            if (!this.form.billId && !this.form.billType && !this.form.billSubmitTime) {
                return this.getPageList(this.tableData, this.tablePage.pageNum, this.tablePage.pageSize)
            } else {
                return this.getPageList(this.tableData.filter((value) => {
                    const idMatch = !this.form.billId || value.billId.includes(this.form.billId);
                    const typeMatch = !this.form.billType || value.billType.includes(this.form.billType);
                    const timeMatch = !this.form.billSubmitTime || value.billSubmitTime.includes(this.form.billSubmitTime);

                    return idMatch && typeMatch && timeMatch;
                }), this.tablePage.pageNum, this.tablePage.pageSize)
            }
        }
    },
    methods: {
        initTableData() {

            billPageQuery(localStorage.getItem('userId')).then(res => {
                this.tableData = res.data.data
            })
        },
        ToBillDetail(row) {
            this.$router.push({
                path: "BillDetail",
                query: {
                    InputUserName: row.userName,
                    InputUserId: row.userId,
                    InputUserPhone: row.userPhone,
                    SelectAutoPlateNum: row.autoPlateNum,
                    SelectBillType: row.billType,
                    InputBillDescribe: row.billDescribe,
                    InputBillTotal: row.billTotal,
                    BillReceipt: row.billReceipt,
                    BillId: row.billId,

                }
            });
        },

        // 分页
        handlePageChange(currentPage) {
            this.tablePage.pageNum = currentPage
            // 在此刷新数据
            this.BasicDataPage = this.getList(this.tableData, this.tablePage.pageNum, this.tablePage.pageSize)
        },
        handleSizeChange(pageSize) {
            this.tablePage.pageSize = pageSize
            // 在此刷新数据
            this.BasicDataPage = this.getList(this.tableData, this.tablePage.pageNum, this.tablePage.pageSize)
        },
        getList(data, num, size) {
            let list, start, end
            start = (num - 1) * size
            end = start + size
            list = data.filter((item, index) => {
                return index >= start && index < end
            })
            list.forEach((item, index) => {
                item.seq = index + start
            })
            return list
        },
        getPageList(data, num, size) {
            this.tablePage.total = data.length
            this.BasicDataPage = this.getList(data, num, size)
            return this.BasicDataPage
        },
        ToAddBillApplication() {
            this.$router.push('/Home/BillApplication')
        },
    },
    mounted() {
        this.initTableData();
    }

}
</script>

<style scoped>
.auto-archive-container {
    height: 100%;
    width: 95%;
    margin: 0 auto;
    margin-top: 1%;
    background-color: #fff;
}

.auto-archive-table {
    width: 90%;
    margin: 10px auto;
}

.auto-archive-title {
    font-size: xx-large;
    justify-content: left;
    width: 10%;
}

.auto-archive-horizon {
    margin: 0 30px;
    height: 1.5px;
    background-color: #ebeef5;
}

.auto-archive-input {
    margin: 0 10px;
    width: 10%;
}

.auto-archive-header {
    display: flex;
    width: 100%;
    margin: 0 auto;
}

.auto-archive-body {
    width: 100%;
    height: 80%;
    margin: 10px auto;
}

.auto-archive-body-header {
    width: 100%;
    margin: 20px auto;
    display: flex;
    align-items: center;
    justify-content: right;
}

.auto-archive-footer {
    width: 70%;
    height: 10%;
    margin: 0 auto;
}

.auto-archive-select {
    margin: 0 auto;
    width: 30%;
}

.auto-archive-select-item {
    width: 99%;
}

.auto-archive-input {
    margin: 0;
    width: 25%;
}

.auto-archive-button {
    margin: 0 20px;
}

.pagination {
    width: 100%;
    margin: 25px auto;
    /* text-align: right; */
}
</style>